//
//  CoreDataViewController.m
//  CoreDataDemo
//
//  Created by fangyuan ding on 2020/8/12.
//  Copyright © 2020 fangyuan ding. All rights reserved.
//

#import "CoreDataViewController.h"
#import "AppDelegate.h"
#import <CoreData/CoreData.h>
#import "Person+CoreDataClass.h"
@interface CoreDataViewController ()
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;

@property (nonatomic, strong)NSManagedObjectContext *context;

@property (nonatomic, strong)AppDelegate *appDelegate;

@end

@implementation CoreDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@", NSHomeDirectory());
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    self.appDelegate = appDelegate;
    NSManagedObjectContext *context =  appDelegate.persistentContainer.viewContext;
    self.context = context;
}
- (IBAction)add:(id)sender {
    Person *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.context];
    person.name = self.nameTextField.text;
    person.phone = self.phoneTextField.text;
    [self.appDelegate saveContext];
    
}

- (IBAction)update:(id)sender {
    //系统自动生成了获取查询请求的方法
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
   //查询条件
   NSPredicate *pre = [NSPredicate predicateWithFormat:@"name = %@", self.nameTextField.text];
     request.predicate = pre;
   NSArray *persons = [self.context executeFetchRequest:request error:nil];
   NSLog(@"%@",persons);
   
    if(persons.count >0){
        Person *p = persons.firstObject;
        p.phone = self.phoneTextField.text;
        [self.appDelegate saveContext];
    }
    
}
- (IBAction)del:(id)sender {
    //系统自动生成了获取查询请求的方法
     NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
    //查询条件
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"name = %@", self.nameTextField.text];
      request.predicate = pre;
    NSArray *persons = [self.context executeFetchRequest:request error:nil];
    NSLog(@"%@",persons);
    if(persons.count >0){
        Person *p = persons.firstObject;
        [self.context deleteObject:p];
         [self.appDelegate saveContext];
              
     }
}

- (IBAction)query:(id)sender {
     //系统自动生成了获取查询请求的方法
     NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
    //排序
    NSSortDescriptor *nameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
  
    [request setSortDescriptors:@[nameDescriptor]];
    //查询条件
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"name = %@", self.nameTextField.text];
      request.predicate = pre;
    NSArray *persons = [self.context executeFetchRequest:request error:nil];
    NSLog(@"%@",persons);
    if(persons.count >0){
        Person *p = persons.firstObject;
        self.phoneTextField.text = p.phone;
              
     }
}
@end
